﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using AutumnEngine.Modules;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Controllers;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Factories;
using FarseerGames.FarseerPhysics.Interfaces;
using FarseerGames.FarseerPhysics.Collisions;
namespace AutumnEngine.Modules
{
    /// <summary>
    /// Holds all scenes physic simulators and gravitic values, etc.
    /// </summary>
    public class PhysicsManagerModule : Module
    {
        
        public Vector2 Gravity
        {
            get;
            set;
        }

        Dictionary<UInt32, PhysicsSimulator> sims;

        internal void AddPhysicsSimulator(UInt32 sceneId)
        {
            if (Engine.Modules.Contains("Engine.Logging"))
                Engine.Modules.Logging.Write("Added simulator for scene " + sceneId.ToString(), LogLevel.Trace);
            PhysicsSimulator s = new PhysicsSimulator(Gravity);
            sims.Add(sceneId, s);
        }

        public override void Create()
        {
            Gravity = new Vector2(0, 9.8f);
            sims = new Dictionary<UInt32, PhysicsSimulator>();
            base.Create();
        }

        public void RemoveSimNodes(UInt32 id)
        {
            sims[id].Clear();
        }

        public override void Update()
        {
            float upd = (float)Engine.GameTime.ElapsedGameTime.Milliseconds * .002f;            
            sims[Engine.Modules.SceneManager.ActiveScene.ID].Update(upd);
            base.Update();
        }

        public void AddNode(UInt32 scene, PhysicsSpriteNode b)
        {
            sims[scene].Add(b.Body);
            sims[scene].Add(b.Geom);
        }
    }
}
